package org.moyakarta.rest;

import java.util.Random;

public class PlaceGenerateUtils {

   private static final String CHAR_SET = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";

   public static String generateEncodedHash() {
      Integer intRandom = new Integer(0);
      while (intRandom.intValue() == 0l) {
         intRandom = new Random().nextInt();
         if (Integer.signum(intRandom) < 0)
            intRandom = -intRandom;
      }
      String result = base48Encode(Long.valueOf(String.valueOf(intRandom)));
      return result;
   }

   private static String base48Encode(Long number) {
      Double num = Double.valueOf(number);

      Integer length = CHAR_SET.length();
      String encodeString = new String();
      while (num >= length) {
         // Long value to int value
         int index = Integer.parseInt(String.valueOf(num.longValue() % length));
         encodeString = CHAR_SET.charAt(index) + encodeString;
         num = Math.ceil(new Double(num / length) - 1);
      }
      encodeString = CHAR_SET.charAt(num.intValue()) + encodeString;

      return encodeString;
   }

   public static float randomFloat(float fromFloat, float toFloat) {

      float diff = toFloat - fromFloat;

      int diffInt = new Float(diff * 100000000).intValue();

      int intRandom = new Random().nextInt();
      int intPercent = intRandom % 10000;

      if (intPercent < 0)
         intPercent = -intPercent;

      int piece = diffInt / 10000;

      float add = piece * intPercent;
      add = add / 100000000l;

      float result = fromFloat + add;
      return result;
   }
   
   public static double randomDouble(double fromDouble, double toDouble) {

      double diff = toDouble - fromDouble;

      int diffInt = new Double(diff * 100000000).intValue();

      int intRandom = new Random().nextInt();
      int intPercent = intRandom % 10000;

      if (intPercent < 0)
         intPercent = -intPercent;

      int piece = diffInt / 10000;

      double add = piece * intPercent;
      add = add / 100000000l;

      double result = fromDouble + add;
      return result;
   }

   public static int randomInt(int fromInt, int toInt) {

      int diff = toInt - fromInt + 1;
      int numPieces = (int) Math.pow(10, Integer.toString(diff).length()) * 10;

      int intRandom = new Random().nextInt();
      int intPercent = intRandom % numPieces;
      if (intPercent < 0)
         intPercent = -intPercent;

      float piece = Float.parseFloat(Integer.toString(diff)) / numPieces;
      float add = piece * intPercent;

      int result = (int) (fromInt + add);
      return result;
   }

}
